<?php
/*
 * Created on Sep 4, 2007 by Gabriel Pratt
 * For LN2_database
 */
 /*
  * Creates a dropdown box.
  * $id is the selected item
  * $array contains all items in the list $keys -> $values
  * returns formated html dropdown box
  */
  
function makeHeader($result, $linkArray = 0) {
	$offset = 0;
	while ($offset < mysql_num_fields($result)) {  //Prints out header column names
		$meta = mysql_fetch_field($result, $offset);
		$offset++;
		if ($linkArray[$meta -> name] == "") { //if the value does not exist in link array print out the name.
			$select .= "<Th>" . $meta ->name . "</th> \n";
		}
	}
	return $select;
}
function fillDropDownBox($array, $id = "") {
	
	foreach ($array as $key => $value) {
			$select .= "<option ";
			$select .= "value=\"$key\"";	
			if($id == $key) {
				$select .=  " selected=\"selected\"";
			}
			
			if(isset($_COOKIE['Debug'])) {	
				$value .= ", ID is: $key";
			} 
			$select .= ">$value</option> \n";						
	}
	return $select;
}
/*
 * $result = result
 * $key = primary key
 * returns formated result in array form where $key is the key of 
 * the array and the rest of the values are in the value spot
 */
function resultToArray($result, $primaryKey) {
	//may want to change back to first item in the query
	while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
		$key = $line[$primaryKey];
		unset($line[$primaryKey]);
		foreach($line as $value) {
			$values .= $value . " ";
		}
		$formatedResult[$key] = $values;
		$values = "";
	}
	//echo "Formated Result is: " . var_dump($formatedResult);
	return $formatedResult;
}

/*
 * Function for easily creating a dropdown box
 * takes a query and a $primaryKey and returns a dropdown box 
 * with out forum information.  
 */
function createDropDownBox ($query, $selected, $primaryKey) {
	selectDataBase();
	$result = mysql_query($query) or die('Query failed in create dropdown box: ' . mysql_error() . "Query: $query");
	$formatedReslut = resultToArray($result, $primaryKey);
	return fillDropDownBox($formatedReslut, $selected);
	
}
/*
 * Creates a view in the form of an actual box.
 * $result is the is the information displayed in 
 * the box format.
 * $linkArray is a array of $values ->websites 
 * where if provided a link will
 * appear instead of information.
 */

function convertToLocation($line, $location) {
	
		if(!$line['LetterHeight'] & !$line['LetterWidth']) {
			return $location;
		}
			
		if($line['numRows']  == 0) {
			$line['numRows'] = 1;
		}
		if($line['numCols'] ==0) {
			$line['numCols'] = 1;
		}
		$row = $location / $line['numRows'] + 1;
		$row -= 1 / $line['numRows'];
		$col = $location % $line['numCols'];
		if($row == $line['numRows']) 
			$row = $line['numRows'] - 1;
	
		if ($col == 0)
			$col = $line['numCols'];
		if($line['LetterHeight']) 
			$row = convertToLetter($row);
			
		if($line['LetterWidth'])
			$col = convertToLetter($col);
		return $row . $col;
}

/*
 * function for creating a page that adds 
 */
function addGeneral($table ,$values, $link, $search) {
	$select .= "<form action=\"$link?$search\" method=\"POST\">";
	$select .= "<input type=\"hidden\" name=\"table\" value=\"$table\">";
	$select .= "<input type=\"hidden\" name=\"id\" value=\"". $_POST['id'] . "\">";
	$select .= "<input type=\"hidden\" name=\"ContainerID\" value=\"". $_POST['ContainerID'] . "\">";
	foreach ($values as $key => $value) {
		//hack to keep from processing further allows me to avoid working to closely with the controller
		$select .= "$key <input type=\"text\" name=\"toAdd" . "[$key]\"> </br> \n";
	}
	$select .= "<input type=\"submit\" name=\"add\" value=\"Add Item\">";
	return $select;
} 


?>
